package com.springboot.chongwu.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.springboot.chongwu.entity.Lingyang;
import com.springboot.chongwu.entity.Yonghu;
import com.springboot.chongwu.entity.view.JuanzengView;
import com.springboot.chongwu.entity.view.LingyangView;
import com.springboot.chongwu.mapper.LingyangMapper;
import com.springboot.chongwu.qpage.QPage;
import com.springboot.chongwu.satoken.StpUserUtil;
import com.springboot.chongwu.service.ILingyangService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.springboot.chongwu.service.IYonghuService;
import com.springboot.chongwu.util.ResultView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 宠物领养表 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-09-25
 */
@Service
public class LingyangServiceImpl extends ServiceImpl<LingyangMapper, Lingyang> implements ILingyangService {

    @Autowired
    private IYonghuService iYonghuService;

    @Override
    public ResultView getPageList(QPage qPage, LingyangView obj) {
        IPage iPage = new Page(qPage.getOffset(), qPage.getLimit());
        List<LingyangView> mapList = this.baseMapper.getPageList(iPage,obj);
        return ResultView.ok(iPage.setRecords(mapList));
    }

    @Override
    public ResultView applove(Lingyang lingyang) {
        Lingyang dto=this.getById(lingyang.getId());
        dto.setUtime(new Date());
        dto.setStatus(lingyang.getStatus());
        dto.setBz(lingyang.getBz());
        this.updateById(dto);
        return ResultView.ok();
    }

    @Override
    public ResultView shenqing(Lingyang lingyang) {
        //判断当前用户有没有捐赠满500
        Yonghu yonghu=this.iYonghuService.getById(StpUserUtil.getLoginIdAsInt());
        if(yonghu.getJzmoney().compareTo(BigDecimal.valueOf(500))==-1){
            return ResultView.error("捐赠不满500元，无法申请领养");
        }
        lingyang.setCtime(new Date());
        lingyang.setStatus(0);
        lingyang.setYid(StpUserUtil.getLoginIdAsInt());
        this.save(lingyang);
        return ResultView.ok();
    }

    @Override
    public ResultView getSelect(LingyangView obj) {
        List<LingyangView> lingyangViewList=this.baseMapper.getSelect(obj);
        return ResultView.ok(lingyangViewList);
    }


}
